package org.cjlm.ui.pub;

import javax.swing.*;
import java.awt.*;

public class QueryItem extends JPanel {
    private String code;
    private String name;

    MLabel label;
    MTextField textField;
    private final JComboBox<String> operatorCombo = new JComboBox<>(new String[]{"等于", "包含"});

    public QueryItem(String code, String name) {
        this.code = code;
        this.name = name;
        setLayout(new GridBagLayout());
        setMaximumSize(new  Dimension(365, 30));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets  = new Insets(2, 5, 2, 5); // 组件间距 (上,左,下,右)
        label = new MLabel(name);
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        gbc.gridx  = 0;
        gbc.gridy  = 0;
        gbc.weightx  = 0.1; // 仅占10%宽度
        gbc.fill  = GridBagConstraints.NONE; // 不拉伸
        gbc.anchor  = GridBagConstraints.LINE_END; // 文本右对齐
        add(label,  gbc);

        gbc.gridx  = 1;
        gbc.weightx  = 0.1; // 占10%宽度
        gbc.fill  = GridBagConstraints.NONE; // 水平拉伸填充
        gbc.anchor  = GridBagConstraints.CENTER; // 文本右对齐
        add(operatorCombo, gbc);

        textField = new MTextField();
        gbc.gridx  = 2;
        gbc.weightx  = 0.8; // 占90%宽度
        gbc.fill  = GridBagConstraints.HORIZONTAL; // 水平拉伸填充
        add(textField,  gbc);
    }

    public String getCondition() {
        String value = textField.getText().trim();
        if (value.isEmpty())  return "";

        String operator = (String) operatorCombo.getSelectedItem();
        // 根据操作符构建不同条件表达式
        if ("等于".equals(operator)) {
            return String.format("column_%s  = '%s'", code,  value);
        } else if ("包含".equals(operator)){
            return String.format("column_%s  LIKE '%%%s%%'", code,  value);
        }
        return null;
    }
}
